package linechart;

import ofc.ChartCreator;
import log4j.LogUtil;

//具体的曲线类型--AI曲线
public class AILineChart {
	// 用户初始化和组合曲线参数
	private LineChartCreator lineChartCreator;
	// 用户往曲线里面塞数据
	private RealAIInfo realAIInfo;

	public AILineChart() {
		if (lineChartCreator == null) {
			lineChartCreator = new LineChartCreator();
			lineChartCreator.init("环境量 - 实时监控");
			// lineChartCreator.setYAxisLimit(0, 100, 10);
			lineChartCreator.setValueCount(100);
			lineChartCreator.setAlarmLimit(20, 80, 100);
			lineChartCreator.setLabelSplit(10);
			LogUtil.debug("初始化一条曲线创建器.");
			// lineChartCreator.setAlarmLimit(-20,
			// 60,lineChartCreator.valueCount);
		}
	}

	public String getName(){
		return realAIInfo.getDevName();
	}
	
	public void setRealAIInfo(RealAIInfo realAIInfo, String color) {
		this.realAIInfo = realAIInfo;

		lineChartCreator.setYAxisLimit(0, 100, 20);
		String title = String.format("%s - 实时监控", realAIInfo.getDevName());
		lineChartCreator.setTitle(title);
		lineChartCreator.addLineChart(realAIInfo.getDevId(), realAIInfo
				.getDevName(), color);
	}

	public void addValue(String deviceId) {
		boolean isOK = realAIInfo.queryInfo();
		lineChartCreator.addValue(realAIInfo.getDevId(), realAIInfo.getValue());
		// 如果获取成功，则显示获取的时间
		// if (isOK) {
		//lineChartCreator.addLabel(realAIInfo.getDevId(), realAIInfo.getLabel());
		// }
		// else {//如果获取失败，则使用当前时间（传null的话，后面的函数会处理成当前时间）
		lineChartCreator.addLabel(realAIInfo.getDevId(), null);
		// }
	}

	public String getChart(String deviceId) {
		addValue(deviceId);
		return lineChartCreator.getChart();
	}

	public ChartCreator getLineChartCreator() {
		return lineChartCreator;
	}

	public void clearHistory(String devId) {
		lineChartCreator.clearHistory(devId);
	}

	public String getChartChange(String deviceId) {
		addValue(deviceId);
		return lineChartCreator.getChartChange(deviceId);
	}
}